package com.bookshop.ui.login;

import java.awt.Color;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.ParseException;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.MaskFormatter;

import com.bookshop.action.IAction;
import com.bookshop.logic.AddressFinder;
import com.bookshop.logic.RemoteServiceAdapter;
import com.bookshop.ui.component.ComponentFactory;
import com.bookshop.ui.component.FontFactory;
import com.bookshop.ui.component.UIConstants;

public class RegisterPanel extends JPanel {
	public static class InvolvedData {
		public String mAddress;
		public String mPhone;
		public String mName;
		public String mPassword;
		public String mAccount;
	}
	
	private JTextField mAccountText;
	private JTextField mPassWordText;
	private JTextField mAddressText;
	private JComboBox<String> mProvince;
	private JComboBox<String> mCity;

	private final LoginPage mPage;
	private final AddressFinder mFinder = new AddressFinder();
	private JTextField mNameText;
	private JFormattedTextField mPhoneText;
	
	public RegisterPanel(LoginPage pLoginPage) {
		mPage = pLoginPage;
		init();
		setcomboBox();
	}
	
	private void init() {
		setBackground(new Color(255, 255, 255));
		setBounds(460, 61, 290, 449);
		setLayout(null);
		
		JLabel title = new JLabel("JOIN IN BOOKHOUSE");
		title.setHorizontalAlignment(SwingConstants.CENTER);
		title.setFont(FontFactory.createBoldFont(UIConstants.TITLE_FONT_SIZE));
		title.setBounds(6, 32, 278, 33);
		add(title);
		
		JLabel account = new JLabel("\u7528\u6237\u540D");
		account.setHorizontalAlignment(SwingConstants.RIGHT);
		account.setBounds(23, 96, 61, 16);
		add(account);
		
		JLabel password = new JLabel("\u5BC6\u7801");
		password.setHorizontalAlignment(SwingConstants.RIGHT);
		password.setBounds(23, 134, 61, 16);
		add(password);
		
		mAccountText = new JTextField(10);
		mAccountText.setBounds(104, 90, 134, 28);
		add(mAccountText);
		
		mPassWordText = new JTextField(10);
		mPassWordText.setBounds(104, 128, 134, 28);
		add(mPassWordText);
		
		MaskFormatter mask = null;
		try {
			mask = new MaskFormatter("###########");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		mPhoneText = new JFormattedTextField(mask);
		mPhoneText.setFocusLostBehavior(JFormattedTextField.PERSIST);
		mPhoneText.setBounds(104, 168, 134, 28);
		add(mPhoneText);

		JLabel phone = new JLabel("\u624B\u673A\u53F7\u7801");
		phone.setHorizontalAlignment(SwingConstants.RIGHT);
		phone.setBounds(23, 174, 61, 16);
		add(phone);
		
		JLabel name = new JLabel("\u771F\u5B9E\u59D3\u540D");
		name.setHorizontalAlignment(SwingConstants.RIGHT);
		name.setBounds(23, 208, 61, 16);
		add(name);
		
		mNameText = new JTextField(10);
		mNameText.setBounds(104, 202, 134, 28);
		add(mNameText);
		
		JLabel address = new JLabel("\u6536\u8D27\u5730\u5740");
		address.setHorizontalAlignment(SwingConstants.RIGHT);
		address.setBounds(23, 243, 61, 16);
		add(address);
		
		mProvince = new JComboBox<String>();
		mProvince.setBounds(104, 239, 134, 27);
		add(mProvince);
		
		mCity = new JComboBox<String>();
		mCity.setBounds(104, 266, 134, 27);
		add(mCity);
		
		mAddressText = new JTextField(10);
		mAddressText.setBounds(46, 298, 215, 28);
		add(mAddressText);
		
		JLabel province = new JLabel("\u7701");
		province.setBounds(235, 242, 61, 16);
		add(province);
		
		JLabel city = new JLabel("\u5E02/\u533A/\u53BF");
		city.setBounds(235, 270, 61, 16);
		add(city);
		
		IAction[] actions = getActions();
		JButton back = ComponentFactory.createEventButton("\u8FD4    \u56DE", actions[0]);
		back.setBounds(46, 354, 94, 29);
		add(back);

		JButton register = ComponentFactory.createEventButton("\u5B8C\u6210\u6CE8\u518C", actions[1]);
		register.setBounds(165, 354, 96, 29);
		add(register);
	}
	
	private void setcomboBox() {
		int[] index = mFinder.getLocalIndex();

		String[] provinces = mFinder.getProvince();
		mProvince.setModel(new DefaultComboBoxModel<String>(provinces));
		mProvince.setSelectedIndex(index[0]);
		
		String[] cities = mFinder.getCity(index[0]);
		mCity.setModel(new DefaultComboBoxModel<String>(cities));
		mCity.setSelectedIndex(index[1]);
		
		mProvince.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				int index = mProvince.getSelectedIndex();
				String[] cities = mFinder.getCity(index);
				mCity.setModel(new DefaultComboBoxModel<String>(cities));
			}
		});
	}
	
	private IAction[] getActions() {
		IAction[] actions = new IAction[2];
		actions[0] = new IAction() {
			
			@Override
			public void act() {
				mPage.loadLoginPanel();
			}
		};
		actions[1] = new IAction() {
			
			@Override
			public void act() {
				InvolvedData data = new InvolvedData();
				data.mAccount = mAccountText.getText();
				data.mName = mNameText.getText();
				data.mPassword = mPassWordText.getText();
				data.mPhone = mPhoneText.getText();
				data.mAddress = mProvince.getSelectedItem().toString() + mCity.getSelectedItem().toString() + mAddressText.getText();
				
				if (RemoteServiceAdapter.instance().register(data)) {
					mPage.loadLoginPanel(mAccountText.getText());
				}
			}
		};
		return actions;
	}

	protected void showMessage(String pMessage) {
		JOptionPane.showMessageDialog(mPage, pMessage);
	}
}

